/**
 *
 */
package net.zantetu.matomeroid.sax;

import java.util.ArrayList;
import java.util.List;

import net.zantetu.matomeroid.dto.ContentDto;
import net.zantetu.matomeroid.dto.TabDto;

import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;

/**
 * @author aono_masashi
 *
 */
public class TabHandler implements ContentHandler {
	private List<TabDto> ret;
	private TabDto currentTabDto;
	private ContentDto currentContentDto;
	private StringBuilder content;
	private int skipDivCnt = 0;

	public TabHandler() {
		ret = new ArrayList<TabDto>();
	}

	public List<TabDto> getResult(){
		return ret;
	}

	@Override
	public void startPrefixMapping(String prefix, String uri)
			throws SAXException {
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes atts) throws SAXException {
		boolean addTag = true;
		if(localName.toUpperCase().equals("DIV")){
			if(atts.getValue("class").equals("tab")) {
				currentTabDto = new TabDto();
				currentTabDto.setTitle(atts.getValue("title"));
				addTag = false;
			}else if(atts.getValue("class").equals("fold")) {
				currentContentDto = new ContentDto();
				currentContentDto.setTitle(atts.getValue("title"));
				content = new StringBuilder();
				addTag = false;
			}else{
				skipDivCnt++;
			}
		}

		if(addTag){
			if(localName.toUpperCase().equals("BR")){
				if(content != null) content.append("<").append(localName).append("/>");
			}else{
				if(content != null) {
					content.append("<").append(localName);
					for(int i=0;i<atts.getLength();i++){
						content.append(" ").append(atts.getQName(i)).append("=\"").append(atts.getValue(i)).append("\"");
					}
					content.append(">");
				}
			}
		}
	}

	@Override
	public void startDocument() throws SAXException {
	}

	@Override
	public void skippedEntity(String name) throws SAXException {
	}

	@Override
	public void setDocumentLocator(Locator locator) {
	}

	@Override
	public void processingInstruction(String target, String data)
			throws SAXException {
	}

	@Override
	public void ignorableWhitespace(char[] ch, int start, int length)
			throws SAXException {
	}

	@Override
	public void endPrefixMapping(String prefix) throws SAXException {
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		if(localName.toUpperCase().equals("DIV")){
			if(skipDivCnt > 0){
				skipDivCnt--;
			}else{
				if(currentContentDto != null){
					currentContentDto.setContent(content.toString());
					currentTabDto.addContent(currentContentDto);
					currentContentDto = null;
				}else{
					ret.add(currentTabDto);
				}
			}
		}
		if(!localName.toUpperCase().equals("BR")){
			content.append("</").append(localName).append(">");
		}
	}

	@Override
	public void endDocument() throws SAXException {

	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		if(content != null) content.append(new String(ch,start,length).trim());
	}
}
